package com.example.mychat.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mychat.comparator.ComparatorTime;
import com.example.mychat.dto.MessageWithThumb;
import com.example.mychat.entity.*;
import com.example.mychat.mapper.AuthoritiesMapper;
import com.example.mychat.mapper.RedPacketMapper;
import com.example.mychat.mapper.ThumbMapper;
import com.example.mychat.service.ICommentService;
import com.example.mychat.service.IMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

@Tag(name = "Feed流获取")
@RestController
@RequestMapping("/fetch")
public class FeedController {
    @Resource
    HttpServletRequest req;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private ICommentService commentService;

    @Resource
    RedPacketMapper redPacketMapper;

    @Resource
    AuthoritiesMapper authoritiesMapper;

    @Resource
    ThumbMapper thumbMapper;

    public int getUserId() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        Authorities authority = authoritiesMapper.selectOne(queryWrapper);
        int userId = authority.getId();
        return userId;
    }

    @Operation(description = "根据发布时间获取消息广场feed流")
    @GetMapping("/time")
    public List<Message> fetchMessageByTime() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        return messageService.findByTime();
    }

    @Operation(description = "根据热度获取消息广场feed流")
    @GetMapping("/likenum")
    public List<MessageWithThumb> fetchMessageByNum() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        return messageService.findByLikeNum();
    }

    @Operation(description = "个性化推荐消息")
    @GetMapping("/recommend")
    public List<MessageWithThumb> recommendMessage() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        return messageService.findByLikeNum(username);
    }

    @Operation(description = "获取朋友圈的消息和红包feed流")
    @GetMapping("/moments")
    public List fetchMessageByReceiverId() {
        int id = getUserId();
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        List<Message> messageList = messageService.findByReceiverId(id);
        System.out.println(messageList);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiverId", id);
        List<Redpacket> redpacketList = redPacketMapper.selectList(queryWrapper);
        //将两个列表合并排序
        List list = new ArrayList();
        Iterator iterator = messageList.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        iterator = redpacketList.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        ComparatorTime comparator = new ComparatorTime();
        Collections.sort(list, comparator.reversed());
        return list;
    }

    @Operation(description = "根据消息id获取评论")
    @GetMapping("/comment/{messageId}")
    public List<Comment> fetchCommentByMessageId(@PathVariable Long messageId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        return commentService.findCommentByMessageId(messageId);
    }

    @Operation(description = "根据消息id获取点赞数")
    @GetMapping("/getLikeNum/{messageId}")
    public Thumb getLikeNumByMessageId(@PathVariable Long messageId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("messageId", messageId);
        Thumb thumb = thumbMapper.selectOne(queryWrapper);
        return thumb;
    }
}
